package com.hhd.flow.controller;

import com.hhd.flow.vo.CompleteUserTaskVo;
import com.hhd.flow.vo.Result;
import com.hhd.flow.engine.FlowEngine;
import com.hhd.flow.engine.cmd.impl.EndCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/flow")
public class EndController {
    @Autowired
    FlowEngine flowEngine;

    @RequestMapping("/end")
    @Transactional
    public Result end(@RequestBody CompleteUserTaskVo completeUserTaskVo) {
        flowEngine.getFlowActionLogService().logForEndProcess(completeUserTaskVo.getFlowUserTaskId(), completeUserTaskVo.getComment());
        EndCommand command = flowEngine.newEndCommandBuilder()
                .flowUserTaskId(completeUserTaskVo.getFlowUserTaskId())
                .model(completeUserTaskVo.getFormModel())
                .comment(completeUserTaskVo.getComment())
                .userSign(completeUserTaskVo.getUserSign())
                .attachmentIds(completeUserTaskVo.getAttachmentIds())
                .commentInfo(completeUserTaskVo.getCommentInfo())
                .approveType(completeUserTaskVo.getApproveType()).build();
        flowEngine.execute(command);
        return Result.ok();
    }
}
